#include "offlineMsgModel.hpp"
#include "database.hpp"

void OfflineMsgModel::Insert(int userid, const std::string& msg)
{
    char sql[1024] = {0};
    sprintf(sql, "insert into offlinemsg values(%d, '%s')", userid, msg.c_str());

    MySQL mysql;
    if (mysql.Connect()) {
        mysql.Update(sql);
    }
}

void OfflineMsgModel::Remove(int userid)
{
    char sql[1024] = {0};
    sprintf(sql, "delete from offlinemsg where userid=%d", userid);

    MySQL mysql;
    if (mysql.Connect()) {
        mysql.Update(sql);
    }
}

std::vector<std::string> OfflineMsgModel::Query(int userid)
{
    char sql[1024] = {0};
    sprintf(sql, "select message from offlinemsg where userid=%d", userid);

    std::vector<std::string> offlineMsg;
    MySQL mysql;
    if (mysql.Connect()) {
        MYSQL_RES* res = mysql.Query(sql);
        if (res != nullptr) {
            MYSQL_ROW row;
            while ((row = mysql_fetch_row(res)) != nullptr) {
                offlineMsg.push_back(row[0]);
            }
            mysql_free_result(res);
        }
    }
    return offlineMsg;
}